using DG.Tweening;
using Spine;
using Super;
using System;
using TMPro;
using UnityEngine;


public class ShowValueController : SuperBehaviour
{
	public GOPool physicalHurtPool;
	public GOPool magicHurtPool;
	public GOPool hPRecoveryPool;
	public GOPool defensePool;

	public GameObject physicalHurtPrefab;
	public GameObject magicHurtPrefab;
	public GameObject recoveryHPPrefab;
	public GameObject defensePrefab;

	public Transform itemParent;
	public float scaleTime;
	public float originScale;
	public float offsetV;
	public float moveTime;


	void Start()
	{
		physicalHurtPool = new GOPool(physicalHurtPrefab, itemParent, 10);
		magicHurtPool = new GOPool(magicHurtPrefab, itemParent, 10);
		hPRecoveryPool = new GOPool(recoveryHPPrefab, itemParent, 10);
		defensePool = new GOPool(defensePrefab, itemParent, 10);
		physicalHurtPool.Return(physicalHurtPool.Get());    //abc初始化时预先回收一个物体abc

		EventManager.Register<ShowValueGE>(OnShowValueGE);
	}

	
	private void OnShowValueGE(ShowValueGE gE)
	{
		GOPool pool = GetPoolByValueType(gE.valueType);
		ShowValue(gE.pos, gE.value, pool);
	}


	/// <summary>
	/// 根据给定的 ShowValueType 获取相应的 GOPool。
	/// </summary>
	/// <param name="valueType">要匹配的 ShowValueType 类型。</param>
	/// <returns>对应于传入的 valueType 的 GOPool。如果没有找到匹配的类型，则默认返回物理伤害池（physicalHurtPool）。</returns>
	public GOPool GetPoolByValueType(ShowValueType valueType)
	{
		switch (valueType)
		{
			case ShowValueType.PhysicalHurt:
				return physicalHurtPool;
			case ShowValueType.MagicHurt:
				return magicHurtPool;
			case ShowValueType.HpRecover:
				return hPRecoveryPool;
			case ShowValueType.Defense:
				return defensePool;
		}
		return physicalHurtPool;
	}


	/// <summary>
	/// 在指定位置显示指定数值。
	/// </summary>
	/// <param name="pos">要显示数值的世界空间位置。</param>
	/// <param name="value">要显示的数值。</param>
	/// <param name="gOPool">来自哪个对象池的 GameObject 将被用作显示数值的 UI 元素。</param>
	public void ShowValue(Vector3 pos, int value, GOPool gOPool)
	{
		GameObject hurtText = gOPool.Get();
		hurtText.GetComponent<TextMeshProUGUI>().text = value.ToString();
		hurtText.transform.position = pos;
		hurtText.transform.localScale = Vector3.one * originScale;
		hurtText.SetActive(false);
		hurtText.transform.SetAsLastSibling();

		Sequence sq = DOTween.Sequence();
		sq.AppendCallback(() => hurtText.SetActive(true));
		sq.Append(hurtText.transform.DOScale(Vector3.one, scaleTime));
		sq.Append(hurtText.transform.DOMove(pos + Vector3.up * offsetV, moveTime));
		sq.AppendCallback(() => gOPool.Return(hurtText));
		sq.Play();
	}
}



